package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 护理项目Service实现类
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);

        Page<NursingProjectVo> nursingProjectVos = (Page<NursingProjectVo>) nursingProjectMapper.selectByPage(name, status);

        return PageResponse.of(nursingProjectVos, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        //校验护理项目名称不能重复
        NursingProject nursingProjectDB = nursingProjectMapper.selectByName(nursingProjectDto.getName());
        if (nursingProjectDB!= null) {
            throw new BaseException(BasicEnum.NP_INSERT_FAIL_REPEAT_NP_REPEAT_NAME);
        }
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addNursingProject(nursingProject);

    }

    @Override
    public NursingProjectVo selectById(Long id) {

        NursingProject nursingProject = nursingProjectMapper.selectById(id);

        return BeanUtil.copyProperties(nursingProject, NursingProjectVo.class);

    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        //判断状态必须是0才可以修改，此处需从数据库查询，前端不返回数据
       Integer status = nursingProjectMapper.selectById(nursingProjectDto.getId()).getStatus();
        if (status!=0) {
            throw new BaseException(BasicEnum.NP_FAIL_INUSE);
        }
        //判断护理项目编号不能重复
        Long count = nursingProjectMapper.countDiffBedNumber(nursingProjectDto.getName(),nursingProjectDto.getId());
        if(count>0){
            throw new BaseException(BasicEnum.NP_INSERT_FAIL_REPEAT_NP_REPEAT_NAME);
        }

        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateNursingProject(nursingProject);

    }

    @Override
    public void updatestatus(Long id, Integer status) {

        nursingProjectMapper.updatestatus(id, status);

    }

    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);

    }

    @Override
    public List<NursingProjectVo> getAllNursingProjects() {
        List<NursingProjectVo> list = nursingProjectMapper.getAllNursingProjects();
        return list;
    }


}